package site.yunnong.atvris.admin.domain;

import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import site.yunnong.atvris.dao.common.constant.AdminConstant;
import site.yunnong.atvris.dao.model.SysAdmin;

import java.util.Arrays;
import java.util.Collection;

/**
 * Spring Security需要的用户详情
 *
 * @author JinghaoZhang
 * @date 2021年-04月-08日
 */
public class AdminDetails implements UserDetails {

    private SysAdmin admin;

    public AdminDetails(SysAdmin admin) {
        this.admin = admin;
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        // 返回当前用户的权限 todo
        return Arrays.asList(new SimpleGrantedAuthority("ROLE_USER"));
    }

    public SysAdmin getAdmin() {
        return this.admin;
    }

    @Override
    public String getPassword() {
        return admin.getPassword();
    }

    @Override
    public String getUsername() {
        return admin.getUsername();
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return admin.getStatus() == AdminConstant.ACTIVE_STATUS;
    }
}
